/**
 * 
 */
package org.mspring.mlog.service.impl.standard;

import java.util.ArrayList;
import java.util.List;

import org.apache.log4j.Logger;
import org.mspring.mlog.core.ServiceFactory;
import org.mspring.mlog.entity.security.TreeItem;
import org.mspring.mlog.service.impl.AbstractInstallService;
import org.mspring.mlog.service.security.TreeItemSecurityService;
import org.mspring.mlog.service.security.TreeItemService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * @author Gao Youbo
 * @since 2012-12-24
 * @Description
 * @TODO
 */
@Service
@Transactional
public class StandardInstallServiceImpl extends AbstractInstallService {

    private static final Logger log = Logger.getLogger(StandardInstallServiceImpl.class);

    /*
     * (non-Javadoc)
     * 
     * @see org.mspring.mlog.service.InstallService#initTreeItems()
     */
    @Override
    public void initTreeItems(Long role) {
        // TODO Auto-generated method stub
        log.debug("init tree items...");
        List<TreeItem> items = new ArrayList<TreeItem>();
        items.add(new TreeItem("1", "博客", "0", "", TreeItem.Type.TREE_FOLDER, true));

        // items.add(new TreeItem("105", "文章管理", "1", "",
        // TreeItem.Type.TREE_ITEM, true));
        // items.add(new TreeItem("105005", "列表", "105", "/admin/post/list",
        // TreeItem.Type.TAB, true));
        // items.add(new TreeItem("105010", "增加", "105", "/admin/post/create",
        // TreeItem.Type.TAB, false));
        // items.add(new TreeItem("105015", "修改", "105", "/admin/post/edit",
        // TreeItem.Type.TAB, false));
        //
        // items.add(new TreeItem("110", "分类管理", "1", "",
        // TreeItem.Type.TREE_ITEM, true));
        // items.add(new TreeItem("110005", "列表", "110", "/admin/catalog/list",
        // TreeItem.Type.TAB, true));
        // items.add(new TreeItem("110010", "增加", "110",
        // "/admin/catalog/create", TreeItem.Type.TAB, false));
        // items.add(new TreeItem("110015", "修改", "110", "/admin/catalog/edit",
        // TreeItem.Type.TAB, false));

        items.add(new TreeItem("105", "文章管理", "1", "", TreeItem.Type.TREE_FOLDER, true));
        items.add(new TreeItem("105005", "分类", "105", "", TreeItem.Type.TREE_ITEM, true));
        items.add(new TreeItem("105005005", "列表", "105005", "/admin/post/catalog/list", TreeItem.Type.TAB, true));
        items.add(new TreeItem("105005010", "增加", "105005", "/admin/post/catalog/create", TreeItem.Type.TAB, false));
        items.add(new TreeItem("105005015", "修改", "105005", "/admin/post/catalog/edit", TreeItem.Type.TAB, false));

        items.add(new TreeItem("105010", "文章", "105", "", TreeItem.Type.TREE_ITEM, true));
        items.add(new TreeItem("105010005", "列表", "105010", "/admin/post/list", TreeItem.Type.TAB, true));
        items.add(new TreeItem("105010010", "增加", "105010", "/admin/post/create", TreeItem.Type.TAB, false));
        items.add(new TreeItem("105010015", "修改", "105010", "/admin/post/edit", TreeItem.Type.TAB, false));
        items.add(new TreeItem("105010020", "配置", "105010", "/admin/post/config", TreeItem.Type.TAB, false));

        items.add(new TreeItem("115", "评论管理", "1", "", TreeItem.Type.TREE_ITEM, true));
        items.add(new TreeItem("115005", "评论管理", "115", "/admin/comment/list", TreeItem.Type.TAB, true));
        items.add(new TreeItem("115010", "反垃圾", "115", "/admin/comment/spam/config", TreeItem.Type.TAB, false));

        items.add(new TreeItem("130", "碎碎念", "1", "", TreeItem.Type.TREE_ITEM, true));
        items.add(new TreeItem("130005", "列表", "130", "/admin/twitter/list", TreeItem.Type.TAB, true));

        items.add(new TreeItem("135", "新闻资讯", "1", "", TreeItem.Type.TREE_FOLDER, true));
        items.add(new TreeItem("135005", "分类", "135", "", TreeItem.Type.TREE_ITEM, true));
        items.add(new TreeItem("135005005", "列表", "135005", "/admin/news/catalog/list", TreeItem.Type.TAB, true));
        items.add(new TreeItem("135005010", "增加", "135005", "/admin/news/catalog/create", TreeItem.Type.TAB, false));
        items.add(new TreeItem("135005015", "修改", "135005", "/admin/news/catalog/edit", TreeItem.Type.TAB, false));
        items.add(new TreeItem("135010", "新闻资讯", "135", "", TreeItem.Type.TREE_ITEM, true));
        items.add(new TreeItem("135010005", "列表", "135010", "/admin/news/list", TreeItem.Type.TAB, true));
        items.add(new TreeItem("135010010", "增加", "135010", "/admin/news/create", TreeItem.Type.TAB, false));
        items.add(new TreeItem("135010015", "修改", "135010", "/admin/news/edit", TreeItem.Type.TAB, false));
        items.add(new TreeItem("135010020", "配置", "135010", "/admin/news/config", TreeItem.Type.TAB, false));

        items.add(new TreeItem("195", "附件管理", "1", "", TreeItem.Type.TREE_ITEM, true));
        items.add(new TreeItem("195005", "列表", "195", "/admin/attachment/list", TreeItem.Type.TAB, true));
        items.add(new TreeItem("195010", "明细", "195", "/admin/attachment/view", TreeItem.Type.TAB, true));

        /**********************************************************************************************************/

        items.add(new TreeItem("2", "论坛", "0", "", TreeItem.Type.TREE_FOLDER, true));
        items.add(new TreeItem("205", "版块", "2", "", TreeItem.Type.TREE_ITEM, true));
        items.add(new TreeItem("205005", "版块分类", "205", "/admin/bbs/forumCatalog/list", TreeItem.Type.TAB, true));
        items.add(new TreeItem("205010", "新增分类", "205", "/admin/bbs/forumCatalog/create", TreeItem.Type.TAB, false));
        items.add(new TreeItem("205015", "修改分类", "205", "/admin/bbs/forumCatalog/edit", TreeItem.Type.TAB, false));
        items.add(new TreeItem("205020", "版块列表", "205", "/admin/bbs/forum/list", TreeItem.Type.TAB, false));
        items.add(new TreeItem("205025", "新增版块", "205", "/admin/bbs/forum/create", TreeItem.Type.TAB, false));
        items.add(new TreeItem("205030", "修改版块", "205", "/admin/bbs/forum/edit", TreeItem.Type.TAB, false));

        items.add(new TreeItem("210", "主题管理", "2", "", TreeItem.Type.TREE_ITEM, true));
        items.add(new TreeItem("210005", "列表", "210", "/admin/bbs/topic/list", TreeItem.Type.TAB, true));
        // items.add(new TreeItem("210010", "修改", "210",
        // "/admin/bbs/topic/edit", TreeItem.Type.TAB, false));

        items.add(new TreeItem("215", "回帖管理", "2", "", TreeItem.Type.TREE_ITEM, true));
        items.add(new TreeItem("215005", "列表", "215", "/admin/bbs/topicReply/list", TreeItem.Type.TAB, true));
        // items.add(new TreeItem("215010", "修改", "215",
        // "/admin/bbs/topicReply/edit", TreeItem.Type.TAB, false));

        /**********************************************************************************************************/

        items.add(new TreeItem("3", "用户/权限", "0", "", TreeItem.Type.TREE_FOLDER, true));
        items.add(new TreeItem("305", "用户管理", "3", "", TreeItem.Type.TREE_ITEM, true));
        items.add(new TreeItem("305005", "列表", "305", "/admin/user/list", TreeItem.Type.TAB, true));
        items.add(new TreeItem("305010", "增加", "305", "/admin/user/create", TreeItem.Type.TAB, false));
        items.add(new TreeItem("305015", "修改", "305", "/admin/user/edit", TreeItem.Type.TAB, false));

        items.add(new TreeItem("310", "角色/授权", "3", "", TreeItem.Type.TREE_ITEM, true));
        items.add(new TreeItem("310005", "列表", "310", "/admin/role/list", TreeItem.Type.TAB, true));
        items.add(new TreeItem("310010", "增加", "310", "/admin/role/create", TreeItem.Type.TAB, false));
        items.add(new TreeItem("310015", "修改", "310", "/admin/role/edit", TreeItem.Type.TAB, false));
        items.add(new TreeItem("310020", "授权", "310", "/admin/role/authorize", TreeItem.Type.TAB, false));

        items.add(new TreeItem("315", "资源管理", "3", "", TreeItem.Type.TREE_ITEM, true));
        items.add(new TreeItem("315005", "列表", "315", "/admin/resource/list", TreeItem.Type.TAB, true));
        items.add(new TreeItem("315010", "增加", "315", "/admin/resource/create", TreeItem.Type.TAB, false));
        items.add(new TreeItem("315015", "修改", "315", "/admin/resource/edit", TreeItem.Type.TAB, false));

        /**********************************************************************************************************/

        /**********************************************************************************************************/

        items.add(new TreeItem("4", "设置", "0", "", TreeItem.Type.TREE_FOLDER, true));

        items.add(new TreeItem("405", "个人信息", "4", "", TreeItem.Type.TREE_ITEM, true));
        items.add(new TreeItem("405005", "个人信息", "405", "/admin/self/info", TreeItem.Type.TAB, true));
        items.add(new TreeItem("405010", "修改头像", "405", "/admin/self/avatar", TreeItem.Type.TAB, false));

        items.add(new TreeItem("410", "网站信息", "4", "", TreeItem.Type.TREE_ITEM, true));
        items.add(new TreeItem("410005", "网站信息", "410", "/admin/setting/siteinfo", TreeItem.Type.TAB, true));

        items.add(new TreeItem("415", "SEO设置", "4", "", TreeItem.Type.TREE_ITEM, true));
        items.add(new TreeItem("415005", "SEO设置", "415", "/admin/setting/seo", TreeItem.Type.TAB, true));

        items.add(new TreeItem("420", "邮件设置", "4", "", TreeItem.Type.TREE_ITEM, true));
        items.add(new TreeItem("420005", "邮件", "420", "/admin/system/mail/setting", TreeItem.Type.TAB, true));
        items.add(new TreeItem("420010", "邮件测试", "420", "/admin/system/mail/test", TreeItem.Type.TAB, false));

        items.add(new TreeItem("430", "固定连接", "4", "", TreeItem.Type.TREE_ITEM, true));
        items.add(new TreeItem("430005", "固定连接", "430", "/admin/system/permalink/config", TreeItem.Type.TAB, true));

        /**********************************************************************************************************/

        items.add(new TreeItem("5", "运营", "0", "", TreeItem.Type.TREE_FOLDER, true));

        items.add(new TreeItem("505", "链接管理", "5", "", TreeItem.Type.TREE_ITEM, true));
        items.add(new TreeItem("505005", "列表", "505", "/admin/link/list", TreeItem.Type.TAB, true));
        items.add(new TreeItem("505010", "增加", "505", "/admin/link/create", TreeItem.Type.TAB, false));
        items.add(new TreeItem("505015", "修改", "505", "/admin/link/edit", TreeItem.Type.TAB, false));
        items.add(new TreeItem("505020", "链接分类", "505", "/admin/linkType/list", TreeItem.Type.TAB, false));
        items.add(new TreeItem("505025", "新增分类", "505", "/admin/linkType/create", TreeItem.Type.TAB, false));
        items.add(new TreeItem("505030", "修改分类", "505", "/admin/linkType/edit", TreeItem.Type.TAB, false));

        items.add(new TreeItem("510", "广告管理", "5", "", TreeItem.Type.TREE_ITEM, true));
        items.add(new TreeItem("510005", "列表", "510", "/admin/ad/list", TreeItem.Type.TAB, true));
        items.add(new TreeItem("510010", "增加", "510", "/admin/ad/create", TreeItem.Type.TAB, false));
        items.add(new TreeItem("510015", "修改", "510", "/admin/ad/edit", TreeItem.Type.TAB, false));

        items.add(new TreeItem("8", "系统", "0", "", TreeItem.Type.TREE_FOLDER, true));
        items.add(new TreeItem("805", "金山快盘", "8", "", TreeItem.Type.TREE_ITEM, true));
        items.add(new TreeItem("805005", "金山快盘", "805", "/admin/api/kuaipan/setting", TreeItem.Type.TAB, true));

        items.add(new TreeItem("815", "采集工具", "8", "", TreeItem.Type.TREE_FOLDER, true));
        items.add(new TreeItem("815005", "采集规则", "815", "", TreeItem.Type.TREE_ITEM, true));
        items.add(new TreeItem("815005005", "列表", "815005", "/admin/spider/rule/list", TreeItem.Type.TAB, true));
        items.add(new TreeItem("815005010", "增加", "815005", "/admin/spider/rule/create", TreeItem.Type.TAB, false));
        items.add(new TreeItem("815005015", "修改", "815005", "/admin/spider/rule/edit", TreeItem.Type.TAB, false));

        items.add(new TreeItem("815010", "发布规则", "815", "", TreeItem.Type.TREE_ITEM, true));
        items.add(new TreeItem("815010005", "列表", "815010", "/admin/spider/publishRule/list", TreeItem.Type.TAB, true));
        items.add(new TreeItem("815010010", "增加", "815010", "/admin/spider/publishRule/create", TreeItem.Type.TAB, false));
        items.add(new TreeItem("815010015", "修改", "815010", "/admin/spider/publishRule/edit", TreeItem.Type.TAB, false));

        items.add(new TreeItem("815015", "文章管理", "815", "", TreeItem.Type.TREE_ITEM, true));
        items.add(new TreeItem("815015005", "列表", "815015", "/admin/spider/post/list", TreeItem.Type.TAB, true));
        items.add(new TreeItem("815015010", "修改", "815015", "/admin/spider/post/edit", TreeItem.Type.TAB, false));
        items.add(new TreeItem("815015015", "查看", "815015", "/admin/spider/post/view", TreeItem.Type.TAB, false));

        items.add(new TreeItem("820", "任务管理", "8", "", TreeItem.Type.TREE_ITEM, true));
        items.add(new TreeItem("820005", "列表", "820", "/admin/system/job/list", TreeItem.Type.TAB, true));
        items.add(new TreeItem("820010", "任务日志", "820", "/admin/system/job/log", TreeItem.Type.TAB, false));

        items.add(new TreeItem("825", "缓存管理", "8", "", TreeItem.Type.TREE_ITEM, true));
        items.add(new TreeItem("825005", "缓存管理", "825", "/admin/system/cache/config", TreeItem.Type.TAB, true));

        items.add(new TreeItem("830", "外观", "8", "", TreeItem.Type.TREE_FOLDER, false));
        items.add(new TreeItem("830005", "皮肤设置", "830", "", TreeItem.Type.TREE_ITEM, true));
        items.add(new TreeItem("830005005", "皮肤设置", "830005", "/admin/setting/skin", TreeItem.Type.TAB, true));
        items.add(new TreeItem("830010", "皮肤编辑", "830", "", TreeItem.Type.TREE_ITEM, true));
        items.add(new TreeItem("830010005", "皮肤编辑", "830010", "/admin/system/skin/list", TreeItem.Type.TAB, true));

        items.add(new TreeItem("890", "数据库监控", "8", "", TreeItem.Type.TREE_ITEM, true));
        items.add(new TreeItem("890005", "数据库监控", "890", "/admin/druid/index.html", TreeItem.Type.TAB, true));

        items.add(new TreeItem("895", "系统日志", "8", "", TreeItem.Type.TREE_FOLDER, false));
        items.add(new TreeItem("895005", "后台管理日志", "895", "", TreeItem.Type.TREE_ITEM, true));
        items.add(new TreeItem("895005005", "列表", "895005", "/admin/log/list", TreeItem.Type.TAB, true));
        items.add(new TreeItem("895005010", "明细", "895005", "/admin/log/view", TreeItem.Type.TAB, false));
        items.add(new TreeItem("895005015", "设置", "895005", "/admin/log/config", TreeItem.Type.TAB, false));
        items.add(new TreeItem("895010", "Ping日志", "895", "", TreeItem.Type.TREE_ITEM, true));
        items.add(new TreeItem("895010005", "列表", "895010", "/admin/log/list", TreeItem.Type.TAB, true));
        items.add(new TreeItem("895010010", "明细", "895010", "/admin/log/view", TreeItem.Type.TAB, false));
        items.add(new TreeItem("895010015", "设置", "895010", "/admin/log/config", TreeItem.Type.TAB, false));

        items.add(new TreeItem("9", "关于", "0", "", TreeItem.Type.TREE_FOLDER, false));
        items.add(new TreeItem("905", "关于", "9", "", TreeItem.Type.TREE_ITEM, true));
        items.add(new TreeItem("905005", "关于", "905", "/admin/about", TreeItem.Type.TAB, true));
        items.add(new TreeItem("910", "联系我们", "9", "", TreeItem.Type.TREE_ITEM, true));
        items.add(new TreeItem("910005", "联系我们", "910", "/admin/contact", TreeItem.Type.TAB, true));

        TreeItemSecurityService treeItemSecurityService = ServiceFactory.getBean(TreeItemSecurityService.class);
        TreeItemService treeItemService = ServiceFactory.getTreeItemService();

        if (role == null) {
            role = new Long(1);
        }
        treeItemSecurityService.clreateRoleTreeItem(role);
        treeItemService.clearItems();
        for (TreeItem item : items) {
            String itemId = treeItemService.createItem(item);
            treeItemSecurityService.setPremission(role, itemId);
        }
    }
}
